package com.eastfair.questionnaire.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.questionnaire.dto.*;
import com.eastfair.questionnaire.service.AnswerResultService;
import com.eastfair.questionnaire.entity.AnswerResult;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.questionnaire.vo.QuestionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 答题结果
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/answerResult")
@Api(value = "AnswerResult", tags = "答题结果")
public class AnswerResultController extends SuperController<AnswerResultService, Long, AnswerResult, AnswerResultPageQuery, AnswerResultSaveDTO, AnswerResultUpdateDTO> {

    private AnswerResultService answerResultService;
    private EchoService echoService;
    @Autowired
    public AnswerResultController(AnswerResultService answerResultService, EchoService echoService) {
        this.answerResultService = answerResultService;
        this.echoService = echoService;
    }

    @Override
    public void handlerResult(IPage<AnswerResult> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<AnswerResult> answerResultList = list.stream().map((map) -> {
            AnswerResult answerResult = AnswerResult.builder().build();
            //TODO 请在这里完成转换
            return answerResult;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(answerResultList));
    }

    @ApiOperation(value = "保存答案", notes = "保存答案")
    @PostMapping("/save")
    public R<Boolean> save (
            @Validated({AnswerSubjectResultSaveDTO.validateSave.class})
            @RequestBody AnswerSubjectResultSaveDTO answerResult) {
        Long projectId = ContextUtil.getProjectId();
        if (CollectionUtil.isEmpty(answerResult.getAnswerResultList())) {
            return R.successDef();
        }
        if (!CollectionUtil.isEmpty(answerResult.getAnswerResultList())) {
            answerResult.getAnswerResultList().stream().forEach(answerResultDTO -> {
                answerResultDTO.setSourceSystem(answerResultDTO.getSourceSystem());
                answerResultDTO.setProjectId(projectId);
                answerResultDTO.setSourceSystem(answerResult.getSourceSystem());
                answerResultDTO.setSubjectType(answerResult.getSubjectType());
                answerResultDTO.setSubjectId(answerResult.getSubjectId());
            });
        }
        answerResultService.save(projectId, answerResult.getSubjectType(), answerResult.getSubjectId(), answerResult.getAnswerResultList());
        return R.successDef();
    }
}
